A good answer might be:

Yes. It is a static method and does not really need a separate class definition.


main() Defined in the myFrame Class

Here is a modification of the previous program. Now there are only two class definitions. The program starts running with the static main() method (as always.) Remember that a static method exists when the program starts up and is not part of any object.

import java.awt.*; 
import java.awt.event.*;
import javax.swing.*;

public class myFrame extends JFrame
{
  public void paint ( Graphics g )
  {
    g.drawString("Click the close button", 10, 50 );  
  }
  
  public static void main ( String[] args )
  {
    myFrame frm = new myFrame();
    
    WindowQuitter wquit = new WindowQuitter(); 
    frm.addWindowListener( wquit );   
    
    frm.setSize( 150, 100 ); 
    frm.setVisible( true ); 
  }
  
}

class WindowQuitter extends WindowAdapter
{
  public void windowClosing( WindowEvent e )
  {
    System.exit( 0 );   
  }
}

The program behaves exactly the same way as before. When you copy and run this program, name the source file myFrame.java (recall:the source file must have the same name as the one public class in that file.)

Most text books write GUI applications this way. There are other tricks that make a GUI program even shorter, and some books use those, too. But the three parts of a GUI program will always be there. Sometimes all three parts will be combined into one object.

QUESTION 15:

Is it OK for main() to ask for a new myFrame() when main() is part of the myFrame class definition?